<?php

namespace App\Http\Middleware;

use App\Models\User;
use Closure;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;

class Authenticate
{
    public function handle(Request $request, Closure $next, ...$guards)
    {


        $token=$request->header('token');
        if(empty($token)){
            return  Json(401,'请登录');
        }
        $userInfo= Cache::get('w_user:'.$token);
        if(empty($userInfo)){
            return  Json(401,'请登录');
        }
        $userInfo=unserialize($userInfo);

        Auth::login(User::query()->where('id','=',$userInfo->id)->first());
        return $next($request);
    }


}
